package com.eagle.security.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;

import com.eagle.security.dao.UserDetailsDao;
import com.eagle.security.entries.RPConstant;
import com.eagle.security.entries.User;
import com.eagle.security.entries.UserCopy;
import com.eagle.security.service.UserManageService;

@Service
public class UserManageServiceImpl implements UserManageService {

    @Resource(name="userSecurityDao")
    private UserDetailsDao userDao;

    @Override
    public int saveUser(User mo) {
        boolean b = userDao.userExists( mo.getUsername() );
        if( b ){    //用户已经存在
            return 0 ;
        }
        userDao.createUser(mo);
        return  1;
    }

    @Override
    @PreAuthorize("hasAnyRole('"+RPConstant.USER_MANAGER+"') or #mo.username == principal.username ")
    public int modifyUser(UserCopy mo) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    	User user = userDao.findUserByUsername(mo.getUsername());
    	if( user !=null ){
    		User user_modify = new User(mo.getUsername(), user.getPassword(), mo.getNickname(), mo.isEnable() );
			PropertyUtils.copyProperties(user_modify, mo);
    		userDao.updateUser(user_modify);
    		return 1;
    	}
        return 0;
    }
    
    public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		User user1 = new User("username", "password","nick", true);
		UserCopy user2 = new UserCopy();
		PropertyUtils.copyProperties(user2, user1);
		System.out.println( user2.getPassword() );
	}

    @Override
    public int deleteUserByUsername(String username) {
        userDao.deleteUser(username);
        return 1;
    }

    @Override
    public User findUserByUsername(String username) {
        return userDao.findUserByUsername( username );
    }

    @Override
    public Map<String, Object> findUserMapByUsername(String id) {
        return userDao.findUserMapByUsername(id);
    }

    @Override
    public List<User> findUsersByCond(User mo, int page, int pageSize) {
        int start = (page - 1) * pageSize;
        return userDao.findUsersByCond(mo, start, pageSize);
    }

    @Override
    public List<Map<String, Object>> findUserMapsByCond(User mo, int page, int pageSize) {
        int start = (page - 1) * pageSize;
        return userDao.findUserMapsByCond(mo, start, pageSize);
    }

    @Override
    public int findUserCountByCond(User mo) {
        return userDao.findUserCountByCond(mo);
    }

    @Override
    public int checkExistWhenSaveUser(User mo) {
        boolean b = userDao.userExists(mo.getUsername());
        return b == true ? 1 : 0 ; 
    }

    
    
}
